package itu.softeng.twit;

import itu.softeng.twit.andro.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class androActivity extends Activity {
	
	private EditText text;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (EditText)findViewById(R.id.editText1);
    }
    
    // This method is called at button click because we assigned the name to the
 	// "On Click property" of the button
    public void myClickHandler(View view) {
    	switch(view.getId()) {
    	case R.id.button1:
    		RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
    		RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
    		
    		if(text.getText().length() == 0) {
    			Toast.makeText(this, "Please enter a valid number",
    					Toast.LENGTH_LONG).show();
    			return;
    		}
    		float inputValue = Float.parseFloat(text.getText().toString());
    		if(celsiusButton.isChecked()) {
    			text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
    			celsiusButton.setChecked(false);
    			fahrenheitButton.setChecked(true);
    		} else {
    			text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
    			fahrenheitButton.setChecked(false);
    			celsiusButton.setChecked(true);
    		}
    		break;
    	
    	/*case android.R.id.home:
    		Intent intent = new Intent(this, OverviewActivity.class);
    		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    		startActivity(intent);
    		break;*/
    	}
    }
    
    // Converts to Celsius
    private float convertFahrenheitToCelsius(float fahrenheit) {
    	return ((fahrenheit - 32) * 5 / 9);
    }
    
    // Converts to Fahrenheit
    private float convertCelsiusToFahrenheit(float celsius) {
    	return ((celsius - 32) * 5 / 9);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.deneme:     Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
                                break;
            case R.id.help:     Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
                                break;
        }
        return true;
    }
}